Repository navigation
fix(tracing): drop trailing comma when serializing baggage without third-party items - #8119
Conversation
…ird-party items Baggage.serialize(include_third_party=True) appended third_party_items unconditionally, so when there were no third-party items it added an empty segment and the serialized header ended with a comma (e.g. "sentry-trace_id=...,sentry-public_key=...,"). This is reachable from the Celery integration, which calls combined.serialize(include_third_party=True) whenever the merged baggage has only Sentry items. Only append third_party_items when it is non-empty; the output is unchanged when third-party items are present. Adds a regression test.
|
Ran the baggage tests locally against this branch — |
chrikrah
left a comment
There was a problem hiding this comment.
@alphasafal CONTRIBUTING auto-closes outside PRs without a linked, maintainer-acknowledged issue. Otherwise, from my perspective this can be merged at e8f8ace.
# probe_celery.py: _update_celery_task_headers({"baggage": "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5"}, span, False)
$ python probe_celery.py # e8f8ace with the base condition (if include_third_party:)
'sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.395125,sentry-environment=production,sentry-transaction=t,sentry-sample_rate=0.5,sentry-sampled=true,'
$ python probe_celery.py # e8f8ace
'sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.051087,sentry-environment=production,sentry-transaction=t,sentry-sample_rate=0.5,sentry-sampled=true'
$ pytest tests/tracing/test_baggage.py tests/integrations/celery/test_update_celery_task_headers.py -q --no-cov
42 passed, 4 warnings in 4.42s # e8f8ace, Python 3.12
1 failed, 41 passed, 4 warnings in 1.11s # base condition restored, test kept
FAILED tests/tracing/test_baggage.py::test_serialize_no_trailing_comma_without_third_party_items
# test_span_with_transaction_custom_headers, the one Celery merge test, sends custom=value
# and expects combined_baggage.serialize(include_third_party=True): it passes on both
non-blocking: add a Celery test without third-party baggage?
Adds a Celery header test where the incoming baggage has only Sentry items. The outgoing baggage must not end with a trailing comma.
chrikrah
left a comment
There was a problem hiding this comment.
@alphasafal the Celery test fails on the base condition; nothing blocks a merge.
$ pytest tests/tracing/test_baggage.py tests/integrations/celery/test_update_celery_task_headers.py -q --no-cov -p no:cacheprovider # export of 0bc5679, sentry_sdk imported from it, Python 3.12.3, celery 5.6.3
43 passed, 4 warnings
2 failed, 41 passed, 4 warnings # base condition `if include_third_party:` restored, tests kept
FAILED tests/tracing/test_baggage.py::test_serialize_no_trailing_comma_without_third_party_items
FAILED tests/integrations/celery/test_update_celery_task_headers.py::test_span_with_transaction_custom_headers_without_third_party_baggage
My 10-25 auto-close warning was wrong: validate-pr.yml only runs on open or reopen, so #8120 needs nothing.
pabloDeputter
left a comment
There was a problem hiding this comment.
Thanks for picking this up! Looks good to me now 😃
Fixes #8120
Summary
Baggage.serialize(include_third_party=True)appendsthird_party_itemsunconditionally. When there are no third-party items (third_party_items == ""), it appends an empty segment, so",".join(...)leaves a trailing comma in the serializedbaggageheader:Per the W3C Baggage spec a trailing comma is an empty list-member (malformed). Sentry's own parser tolerates it, but a strict W3C / OpenTelemetry baggage parser on a downstream consumer can reject or mishandle the header.
Where it's reachable
The Celery integration calls
combined.serialize(include_third_party=True)(sentry_sdk/integrations/celery/__init__.py) when merging incoming and Sentry baggage. Whenever the merged baggage has only Sentry items and no third-party ones — the common Sentry → Celery → Sentry propagation case — the outgoingbaggageheader gets the stray comma.Fix
Only append
third_party_itemswhen it is non-empty:The output is unchanged when third-party items are present.
Test
Adds
test_sentry_only_baggageintests/tracing/test_baggage.py: it verifies that baggage containing only Sentry items serializes to the expected header, with identical output whether third-party items are included or not.Validation on Python 3.13.7:
TESTPATH=tests/tracing/test_baggage.py uv run tox -e py3.13-common -- --no-cov: 4 passed.if include_third_party:condition makestest_sentry_only_baggagefail on the trailing comma (1 failed, 3 passed); restoring the fix gives 4 passed.testsandsentry_sdk; formatting checks passed for the changed test files.uv run --group typing mypy sentry_sdk: no issues in 203 source files.